package com.file.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.childmeter.service.ChildmeterService;
import com.entity.Collector;
import com.entity.CustomerMeterPK;
import com.entity.Measuringpoint;
import com.entity.Meter;
import com.entity.Org;
import com.file.entity.FileCustomerMeter;
import com.file.entity.FileMeter;
import com.interfaces.ChildMeterDao;
import com.interfaces.CollectorDao;
import com.interfaces.CustomerDao;
import com.interfaces.CustomerMeterDao;
import com.interfaces.MeasuringpointDao;
import com.interfaces.MeterDao;
import com.interfaces.OrgDao;
import com.interfaces.SysdicDao;
import com.interfaces.TerminalDao;
import com.setting.service.SysdicService;
import com.utils.JsonResult;
import com.utils.Utils;

/**
 * @Title: MeterService.java
 * @Package com.service
 * @author 孙梦楠
 * @date 2017年7月21日 上午11:37:27
 * @version V1.0.0
 */

@Service
public class MeterService {

	@Autowired
	private MeterDao meterdao;

	@Autowired
	private SysdicDao sysdicdao;

	@Autowired
	private MeasuringpointDao measuringpointdao;

	@Autowired
	private CustomerMeterDao customermeterdao;

	@Autowired
	private CustomerDao customerdao;

	@Autowired
	private CollectorDao collectordao;

	@Autowired
	private TerminalDao terminaldao;

	@Autowired
	private SysdicService sysdicservice;

	@Autowired
	private ChildmeterService childmeterservice;

	@Autowired
	private ChildMeterDao childmeterdao;

	@Autowired
	private OrgDao orgdao;

	private static final Logger log = LoggerFactory.getLogger(MeterService.class);

	// 列表
	@Transactional

	public JsonResult<List<FileMeter>> listmeter(String opeorgcookie, String opetypecookie, String languagecookie) {

		List<Meter> meters = meterdao.findByMeterorgAndTypeAndMeterstatusNot(Integer.valueOf(opeorgcookie),
				opetypecookie, "4");
		List<FileMeter> fileMeters = new ArrayList<FileMeter>();
		if (meters.size() != 0) {
			for (int i = 0; i < meters.size(); i++) {
				Meter meter = meters.get(i);
				fileMeters.add(dofilemeter(meter, languagecookie));
			}
		}
		return new JsonResult<List<FileMeter>>(63, "展示表所有列表", fileMeters);
	}

	// 根据状态列出表
	@Transactional
	public JsonResult<List<FileMeter>> listallmeter(String opeorgcookie, String opetypecookie, String Meterstatus,
			Integer State, String Massage, String languagecookie) {

		List<Meter> meters = meterdao.findByMeterorgAndTypeAndMeterstatus(Integer.valueOf(opeorgcookie), opetypecookie,
				Meterstatus);
		List<FileMeter> fileMeters = new ArrayList<FileMeter>();
		if (meters.size() != 0) {
			for (int i = 0; i < meters.size(); i++) {
				Meter meter = meters.get(i);
				fileMeters.add(dofilemeter(meter, languagecookie));
			}
		}
		return new JsonResult<List<FileMeter>>(State, Massage, fileMeters);
	}

	// 条件列表
	@Transactional
	public JsonResult<List<FileMeter>> listMeterByConditions(String metercode, String meterassetcode, String metertype,
			String meteraddress, String meterprotocol, String metermade, String opeorgcookie, String opetypecookie,
			String meterstatus, String meterpassword, String languagecookie) {

		log.info("metercode   " + metercode);
		log.info("meterassetcode   " + meterassetcode);
		log.info("metertype   " + metertype);
		log.info("meteraddress   " + meteraddress);
		log.info("meterprotocol   " + meterprotocol);
		log.info("metermade   " + metermade);
		log.info("opeorgcookie   " + opeorgcookie);
		log.info("meterstatus   " + meterstatus);
		log.info("meterpassword   " + meterpassword);
		log.info("opetypecookie   " + opetypecookie);

		if (Utils.isNull(metercode) && Utils.isNull(meterassetcode) && Utils.isNull(metertype)
				&& Utils.isNull(meteraddress) && Utils.isNull(meterprotocol) && Utils.isNull(metermade)
				&& Utils.isNull(meterstatus) && Utils.isNull(meterpassword)) {
			List<FileMeter> fileMeters = new ArrayList<FileMeter>();

			if (opeorgcookie.equals("1")) {
				List<Meter> meters = meterdao.findByTypeAndMeterstatusNot(opetypecookie, "4");
				for (Meter meter : meters) {
					fileMeters.add(dofilemeter(meter, languagecookie));
				}
			} else if (orgdao.findByOrgvalue(opeorgcookie).getOrgfather().equals("0")) {
				List<Org> orgs = orgdao.findByOrgfather(opeorgcookie);
				for (Org org : orgs) {

					List<Meter> meters = meterdao.findByMeterorgAndTypeAndMeterstatusNot(
							Integer.parseInt(org.getOrgvalue()), opetypecookie, "4");

					for (Meter meter : meters) {
						fileMeters.add(dofilemeter(meter, languagecookie));
					}
				}
			} else {

				List<Meter> meters = meterdao.findByMeterorgAndTypeAndMeterstatusNot(Integer.valueOf(opeorgcookie),
						opetypecookie, "4");
				if (meters.size() != 0) {
					for (int i = 0; i < meters.size(); i++) {
						Meter meter = meters.get(i);
						fileMeters.add(dofilemeter(meter, languagecookie));
					}
				}
			}
			log.info("listMeterSuccess");
			return new JsonResult<List<FileMeter>>(63, "展示表所有列表", fileMeters);

		} else {
			List<FileMeter> fileMeters = new ArrayList<FileMeter>();

			if (opeorgcookie.equals("1")) {
				List<Meter> meters = meterdao.findByConditions(metercode, meterassetcode, metertype, meteraddress,
						meterprotocol, metermade, null, meterstatus, meterpassword, opetypecookie);
				for (Meter meter : meters) {
					fileMeters.add(dofilemeter(meter, languagecookie));
				}
			} else if (orgdao.findByOrgvalue(opeorgcookie).getOrgfather().equals("0")) {
				List<Org> orgs = orgdao.findByOrgfather(opeorgcookie);
				for (Org org : orgs) {
					List<Meter> meters = meterdao.findByConditions(metercode, meterassetcode, metertype, meteraddress,
							meterprotocol, metermade, Integer.parseInt(org.getOrgvalue()), meterstatus, meterpassword,
							opetypecookie);

					for (Meter meter : meters) {
						fileMeters.add(dofilemeter(meter, languagecookie));
					}
				}
			} else {
				List<Meter> meters = meterdao.findByConditions(metercode, meterassetcode, metertype, meteraddress,
						meterprotocol, metermade, Integer.valueOf(opeorgcookie), meterstatus, meterpassword,
						opetypecookie);

				if (meters.size() != 0) {
					for (int i = 0; i < meters.size(); i++) {
						Meter meter = meters.get(i);
						fileMeters.add(dofilemeter(meter, languagecookie));
					}
				}
			}
			log.info("listMeterByConditionsSuccess");
			return new JsonResult<List<FileMeter>>(70, "展示表条件查询列表", fileMeters);

		}
	}

	// 增加
	@Transactional
	public JsonResult<Meter> addmeter(String metercode, String meterassetcode, String metertype, String meteraddress,
			String meterprotocol, String metermade, String opeorgcookie, String meterpassword, String opetypecookie,
			Integer metermultiple, String meterphysicaladdress, String meterkeytype) {

		// log.info("metercode " + metercode);
		// log.info("meterassetcode " + meterassetcode);
		// log.info("metertype " + metertype);
		// log.info("meteraddress " + meteraddress);
		// log.info("meterprotocol " + meterprotocol);
		// log.info("metermade " + metermade);
		// log.info("opeorgcookie " + opeorgcookie);
		// log.info("meterpassword " + meterpassword);

		if (Utils.notNullAndEmpty(metercode) && Utils.notNullAndEmpty(meterassetcode)
				&& Utils.notNullAndEmpty(metertype) && Utils.notNullAndEmpty(meteraddress)
				&& Utils.notNullAndEmpty(meterprotocol) && Utils.notNullAndEmpty(metermade)
				&& Utils.notNullAndEmpty(meterpassword) && Utils.notNullAndEmpty(opeorgcookie)
				&& Utils.notNullAndEmpty(opetypecookie) && Utils.notNullAndEmpty(meterphysicaladdress)) {

			if (meterdao.findByMetercodeAndMeterstatusNot(metercode, "4") == null) {

				if (meterdao.findByMeterassetcodeAndMeterstatusNot(meterassetcode, "4") == null) {

					if (meterdao.findByMeteraddressAndMeterstatusNot(meteraddress, "4") == null) {

						Meter meter = new Meter();
						meter.setMetercode(metercode);
						meter.setMeterassetcode(meterassetcode);
						meter.setMetertype(metertype);
						meter.setMeteraddress(meteraddress);
						meter.setMeterphysicaladdress(meterphysicaladdress);
						meter.setMeterprotocol(meterprotocol);
						meter.setMetermade(metermade);
						meter.setMeterorg(Integer.valueOf(opeorgcookie));
						meter.setMeterstatus("1");
						meter.setMeterpassword(meterpassword);
						meter.setType(opetypecookie);
						if (metermultiple == null) {
							meter.setMetermultiple(1);
						} else {
							meter.setMetermultiple(metermultiple);
						}
						if (meterkeytype == null) {
							meter.setMeterkeytype("0");// 默认公钥
						} else {
							meter.setMeterkeytype(meterkeytype);
						}
						meter.setMetermoney(0.00);
						meterdao.save(meter);

						return new JsonResult<Meter>(61, "增加表成功", meter);
					}
					return new JsonResult<Meter>(73, "增加表失败..通讯地址不能重复", null);
				}
				return new JsonResult<Meter>(68, "增加表失败..资产编码不能重复", null);
			}
			return new JsonResult<Meter>(71, "增加表失败..出厂编码不能重复", null);
		}

		return new JsonResult<Meter>(62, "增加表失败..数值不能为空", null);
	}

	// 删除
	@Transactional
	public JsonResult<Meter> deletemeter(Integer meterid) {
		if (meterid != null) {

			if (meterdao.findOne(meterid).getMeterstatus().equals("1")) {

				meterdao.updateMeter("4", meterid);

				return new JsonResult<Meter>(64, "删除表成功", null);
			} else if (meterdao.findOne(meterid).getMeterstatus().equals("2")) {

				return new JsonResult<Meter>(77, "删除单个表失败..该表正在与用户连接", null);
			} else if (meterdao.findOne(meterid).getMeterstatus().equals("3")) {

				return new JsonResult<Meter>(78, "删除单个表失败..该表正在与采集点连接", null);
			}
		}
		return new JsonResult<Meter>(76, "删除单个表失败..ID为空", null);
	}

	// 强制删除
	@Transactional
	public JsonResult<Meter> forcedeletemeter(Integer meterid) {
		if (meterid != null) {
			// 设置表状态为拆除
			meterdao.updateMeter("4", meterid);
			// 删除总分表状态
			if (childmeterdao.findOne(meterid) != null) {
				childmeterservice.delect1(meterid, childmeterdao.findOne(meterid).getType());
			}

			// 移除用户和表 关联
			if (customermeterdao.findByMeterid(meterid) != null) {

				// 转换为object类
				Object object = customermeterdao.findByMeterid(meterid);
				// 强行转换为PK
				CustomerMeterPK pk = (CustomerMeterPK) object;
				Integer customerid = pk.getCustomerid();

				// 如果用户只有一个表，修改用户状态
				if (customermeterdao.findByCustomerid(customerid).size() == 1) {
					customerdao.updateCustomer("1", customerid);
				}

				customermeterdao.deleteByMeterid(meterid);

			}
			// 移除采集点关联
			if (measuringpointdao.findByMeterid(meterid) != null) {
				Integer measuringpointid = measuringpointdao.findByMeterid(meterid).getMeasuringpointid();
				// 判断采集器还有没有表
				List<Measuringpoint> measuringpoints1 = measuringpointdao
						.findByCollectorid(measuringpointdao.findOne(measuringpointid).getCollectorid());
				if (measuringpoints1.size() == 1) {

					collectordao.updateCollector("1", measuringpointdao.findOne(measuringpointid).getCollectorid());

				}
				// 如果集中器没有连接，更改采集终端状态
				List<Measuringpoint> measuringpoints2 = measuringpointdao
						.findByTerminalid(measuringpointdao.findOne(measuringpointid).getTerminalid());
				if (measuringpoints2.size() == 1) {

					terminaldao.updateTerminal("1", measuringpointdao.findOne(measuringpointid).getTerminalid());

				}

				measuringpointdao.deleteByMeterid(meterid);

			}
			return new JsonResult<Meter>(64, "删除表成功", null);
		}
		return new JsonResult<Meter>(76, "删除单个表失败..ID为空", null);
	}

	// 修改页面
	public JsonResult<FileMeter> updateviewmeter(Integer meterid, String languagecookie) {
		if (meterid != null) {
			Meter meter = meterdao.findOne(meterid);
			log.info("meterid   " + meterid);
			log.info("getoneSuccess");
			return new JsonResult<FileMeter>(65, "查单个表", dofilemeter(meter, languagecookie));
		}
		return new JsonResult<FileMeter>(75, "查单个表失败..ID为空", null);

	}

	// 修改
	@Transactional
	public JsonResult<Meter> updatemeter(Integer meterid, String metercode, String meterassetcode, String metertype,
			String meteraddress, String meterprotocol, String metermade, String opeorgcookie, String meterstatus,
			String meterpassword, String opetypecookie, Integer metermultiple, String meterphysicaladdress,
			String meterkeytype) {

		Meter meter = new Meter();

		if (meterid == null || Utils.isNull(opeorgcookie) || Utils.isEmpty(metercode) || Utils.isEmpty(meterassetcode)
				|| Utils.isEmpty(metertype) || Utils.isEmpty(meteraddress) || Utils.isEmpty(meterprotocol)
				|| Utils.isEmpty(metermade) || Utils.isEmpty(meterstatus) || Utils.isEmpty(meterpassword)
				|| Utils.isNull(opetypecookie) || Utils.isNull(meterphysicaladdress) || Utils.isNull(meterkeytype)) {

			log.info("UpdateFail");
			return new JsonResult<Meter>(67, "修改表失败..数值不能为空", null);
		} else {
			meter.setMeterid(meterid);
			meter.setMeterorg(Integer.valueOf(opeorgcookie));
			meter.setType(opetypecookie);

			// 判断metercode
			if (Utils.isNull(metercode)) {
				meter.setMetercode(meterdao.findOne(meterid).getMetercode());

			} else if (meterdao.findByMetercodeAndMeterstatusNot(metercode, "4") != null
					&& !(meterdao.findOne(meterid).getMetercode().equals(metercode))) {
				log.info("UpdateFail");
				return new JsonResult<Meter>(72, "修改表失败..出厂编码不能重复", null);

			} else {
				meter.setMetercode(metercode);
			}
			// 判断meterassetcode
			if (Utils.isNull(meterassetcode)) {
				meter.setMeterassetcode(meterdao.findOne(meterid).getMeterassetcode());

			} else if (meterdao.findByMeterassetcodeAndMeterstatusNot(meterassetcode, "4") != null
					&& !(meterdao.findOne(meterid).getMeterassetcode().equals(meterassetcode))) {
				log.info("UpdateFail");
				return new JsonResult<Meter>(69, "修改表失败..资产编码不能重复", null);

			} else {
				meter.setMeterassetcode(meterassetcode);
			}
			// 判断meteraddress
			if (Utils.isNull(meteraddress)) {
				meter.setMeteraddress(meterdao.findOne(meterid).getMeteraddress());

			} else if (meterdao.findByMeteraddressAndMeterstatusNot(meteraddress, "4") != null
					&& !(meterdao.findOne(meterid).getMeteraddress().equals(meteraddress))) {
				log.info("UpdateFail");
				return new JsonResult<Meter>(74, "修改表失败..通讯地址不能重复", null);

			} else {
				meter.setMeteraddress(meteraddress);
			}
			// 判断meterphysicaladdress
			if (Utils.isNull(meterphysicaladdress)) {
				meter.setMeterphysicaladdress(meterdao.findOne(meterid).getMeterphysicaladdress());

			} else {
				meter.setMeterphysicaladdress(meterphysicaladdress);

			}
			// 判断metertype
			if (Utils.isNull(metertype)) {
				meter.setMetertype(meterdao.findOne(meterid).getMetertype());

			} else {
				meter.setMetertype(metertype);

			}
			// 判断meterprotocol
			if (Utils.isNull(meterprotocol)) {
				meter.setMeterprotocol(meterdao.findOne(meterid).getMeterprotocol());

			} else {
				meter.setMeterprotocol(meterprotocol);

			}
			// 判断metermade
			if (Utils.isNull(metermade)) {
				meter.setMetermade(meterdao.findOne(meterid).getMetermade());

			} else {
				meter.setMetermade(metermade);

			}
			// 判断meterstatus
			if (Utils.isNull(meterstatus)) {
				meter.setMeterstatus(meterdao.findOne(meterid).getMeterstatus());

			} else {
				meter.setMeterstatus(meterstatus);

			}
			// 判断metermultiple
			if (metermultiple == null) {
				meter.setMetermultiple(meterdao.findOne(meterid).getMetermultiple());

			} else {
				meter.setMetermultiple(metermultiple);

			}
			// 判断meterpassword
			if (Utils.isNull(meterpassword)) {
				meter.setMeterpassword(meterdao.findOne(meterid).getMeterpassword());

			} else {
				meter.setMeterpassword(meterpassword);

			}
			// 判断meterkeytype
			if (Utils.isNull(meterkeytype)) {
				meter.setMeterkeytype(meterdao.findOne(meterid).getMeterkeytype());

			} else {
				meter.setMeterkeytype(meterkeytype);

			}
			meterdao.save(meter);
			log.info("UpdateSuccess");
			return new JsonResult<Meter>(66, "修改表成功", meter);
		}
	}

	// 组装前端类
	public FileMeter dofilemeter(Meter meter, String languagecookie) {

		FileMeter filemeter = new FileMeter();
		// 添加基本信息

		filemeter.setMeterid(meter.getMeterid());
		filemeter.setMetercode(meter.getMetercode());
		filemeter.setMeterassetcode(meter.getMeterassetcode());
		filemeter.setMetertype(meter.getMetertype());
		filemeter.setMetertypename(sysdicservice.finddisplay(sysdicservice.gettype(meter.getType(), "3"),
				meter.getMetertype(), languagecookie));
		filemeter.setMeteraddress(meter.getMeteraddress());
		filemeter.setMeterprotocol(meter.getMeterprotocol());
		filemeter.setMeterprotocolname(sysdicservice.finddisplay("0011", meter.getMeterprotocol(), languagecookie));
		filemeter.setMetermade(meter.getMetermade());
		filemeter.setMetermadename(sysdicservice.finddisplay("0008", meter.getMetermade(), languagecookie));
		filemeter.setMeterorg(meter.getMeterorg());
		filemeter.setMeterorgname(sysdicservice.finddisplay("0001", meter.getMeterorg().toString(), languagecookie));
		filemeter.setMeterstatus(meter.getMeterstatus());
		filemeter.setMeterstatusname(sysdicservice.finddisplay("0009", meter.getMeterstatus(), languagecookie));
		filemeter.setMeterpassword(meter.getMeterpassword());
		filemeter.setMeterkeytype(sysdicservice.finddisplay("0031", meter.getMeterkeytype(), languagecookie));
		return filemeter;

	}

	// 传入表ID查询表地址

	public String findMeteraddressByMeterid(Integer Meterid) {

		String Meteraddress = meterdao.findOne(Meterid).getMeteraddress();

		return Meteraddress;
	}

	public String findMeterprotocolByMeterid(Integer Meterid) {
		String Meterprotocol = meterdao.findOne(Meterid).getMeterprotocol();
		return Meterprotocol;
	}

}
